<?php

require_once "lib/controller.php";

class AdminController extends BaseController {
    
    public function index($route) {
      include("conf/routes.php");
      if ($this->isAdmin()) {
        $this->render_view("index",array("controller" => "admin"));
      } else {
        $this->render_error("error", "Not Identified", $route);
      }
    }
    
    public function adduser($route) {
        if (isset($_POST["user"]) && isset($_POST["password"]) && $this->isAdmin()) {
            include("lib/config.php");
            $hash = hash("sha512", $_POST["user"].$_POST["password"]);
            mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());
            
            $verif = mysql_query(sprintf("SELECT * FROM Users WHERE name='%s'",$_POST["user"])) or die(mysql_error());
            $info = mysql_fetch_array($verif);
            if(strcmp($info["name"],$_POST["user"]) == 0) {
              $this->render_error("error", "User déjà ajouté", $route);
            } else {
              $enr = mysql_query(sprintf("INSERT INTO Users(name, password_hash, is_admin) VALUES('%s','%s','0')",$_POST["user"],$hash));
              if ($enr==true){ 
                $this->render_error("success", sprintf("Utilisateur %s ajouté avec succès", $_POST['user']), $route);
              } 
              else { 
                echo mysql_error(); 
              } 
            }
        } else {
          $this->render_error("error", "Not Identified request", $route);
        }
    }
    
    public function rmuser($route) {
      if($this->isAdmin()){
        if (isset($_POST["user"])) { 
            include("lib/config.php");
            mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());

            $verif = mysql_query(sprintf("SELECT * FROM Users WHERE name='%s'",$_POST["user"])) or die(mysql_error());
            $info = mysql_fetch_array($verif);
            if(strcmp($info["name"],$_POST["user"]) != 0) {
              $this->render_error("error", "User non existant", $route);
            } else {
              if(mysql_query(sprintf("DELETE FROM Users WHERE name= '%s'",$_POST["user"]))){
                $this->render_error("success", sprintf("Utilisateur %s supprimé", $_POST['user']), $route);
              } else {
                echo mysql_error();
              }
            }
          } else {
              $this->render_error("error", "No user in POST", $route);
          }
        } else {
          $this->render_error("error", "Not Identified request", $route);
        }
    }

    public function setFailure($route){
      if($this->isAdmin()){
        if (isset($_POST["machine"])) {
          include("lib/config.php");
            mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());
            
            $verif = mysql_query(sprintf("SELECT COUNT(id_machine) FROM Machines WHERE id_machine='%s'",$_POST["machine"])) or die(mysql_error());
            $info = mysql_fetch_array($verif);
            if((int) $info["COUNT(id_machine)"] != 1) {
              $this->render_error("error", "Machine non existante", $route);
            } else {
              mysql_query(sprintf("UPDATE Machines SET state = 'failure' WHERE id_machine= %s",$_POST["machine"])) or die(mysql_error());

              $this->render_error("success", sprintf("Etat de la machine %s mis à jour", $_POST["machine"]), $route);
            }
        } else {
          $this->render_error("error", "Uncomplete request", $route);
        }
      } else {
        $this->render_error("error", "Not Identified request", $route);
      }
    }

    public function setWorking($route){
      if($this->isAdmin()){
        if (isset($_POST["machine"])) {
          include("lib/config.php");
            mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
            mysql_select_db($db_name) or die(mysql_error());
            
            $verif = mysql_query(sprintf("SELECT COUNT(id_machine) FROM Machines WHERE id_machine='%s'",$_POST["machine"])) or die(mysql_error());
            $info = mysql_fetch_array($verif);
            if((int) $info["COUNT(id_machine)"] != 1) {
              $this->render_error("error", "Machine non existante", $route);
            } else {
              mysql_query(sprintf("UPDATE Machines SET state = 'working' WHERE id_machine= %s",$_POST["machine"])) or die(mysql_error());

              $this->render_error("success", sprintf("Etat de la machine %s mis à jour", $_POST["machine"]), $route);
            }
        } else {
          $this->render_error("error", "Uncomplete request", $route);
        }
      } else {
        $this->render_error("error", "Not Identified request", $route);
      }
    }
}

?>
